<template>
	<view>
		<view class="c_navbar">
			<u-navbar :is-back="false" title="钻机管理系统"></u-navbar>
			<view class="navbar-content">
				<view class="left-avatar u-text-center">
					<u-avatar size="100" :src="userInfo.avatarUrl || ''"></u-avatar>
					<template v-if="hasLogin">
						<view class="username">昵称：{{ userInfo.nickName }}</view>
						<view class="username">角色：{{ userType === 'wk' ? '员工' : '项目经理' }}</view>
					</template>
					<view v-else class="username">请点击右边按钮登录</view>
				</view>
				<view class="right-options u-flex-1 u-text-right">
					<button type="primary" size="mini" open-type="getUserInfo" @click="login" v-if="!hasLogin">点击登录</button>
					<button type="warn" size="mini" v-else @click="signout">安全退出</button>
				</view>
			</view>
		</view>
		
		<view class="wrap">
			<u-row gutter="40" justify="around">
				<block v-for="(item,index) in authData[userType]" :key="index">
					<template>
						<u-col span="6" @click="goPage(item)">
							<view hover-class="h-class" class="demo-layout">
								<view :class="['icon iconfont',item.icon]"></view>
								<view class="u-font-xl u-margin-top-20 nav-title">{{item.title}}</view>
							</view>
						</u-col>
					</template>
				</block>
			</u-row>
		</view>
		<!-- 消息提示 -->
		<u-toast ref="uToast" />
		<!-- 模态框 -->
		<u-modal show-cancel-button="true" v-model="modalDisabled" @confirm="confirmOut" content="确认安全退出吗？"></u-modal>
	</view>
</template>

<script>
	// 根据不同权限获取导航列表
	import {authData} from "@/common/authority.js";
	import {mapState,mapMutations} from "vuex";
	export default {
		data() {
			return {
				authData,
				modalDisabled:false
			}
		},
		computed:{
			...mapState({
				hasLogin:'hasLogin',
				userID:"userID",
				userInfo:'userInfo',
				token:"token",
				userType:"userType",
				wkID:"wkID"
			})
		},
		methods: {
			...mapMutations(['signing','logout']),
			goPage(item){
				let { userType,wkID,hasLogin } = this;
				if(!this.hasLogin) {
					this.$refs.uToast.show({title: '请先登录',type: 'error'})
					return false;
				}
				//未添加个人信息前只能录入个人信息
				if(wkID == "" && item.title != "个人信息"){
					this.$refs.uToast.show({title: '请先录入个人信息',type: 'error'})
					return false;
				}
				uni.navigateTo({url: item.routerPath});
			},
			signout(){
				this.modalDisabled = true
			},
			confirmOut(){
				this.logout();
			},
			login(){
				// #ifdef MP-WEIXIN
				uni.getUserInfo({
					provider: "weixin",
					success: async (user) => {
						let res = await this.$loginMP();
						let {avatarUrl,nickName} = user.userInfo;
						let {token,userID} = res.result;
						let userType,wkID,pm_company;
						// 获取用户类型
						let userInfoRes = await this.$network({
							api:"main",
							data:{
								action:"user/getUserInfo",
								data:{
									_userID:userID,
									token
								}
							}
						})

						if(userInfoRes.status === 0){
							userType = userInfoRes.data[0].userType;
							wkID = userInfoRes.data[0].wkID;
							pm_company = userInfoRes.data[0].pm_company;
						} else {
							userType = "wk";
							wkID = "";
							this.$refs.uToast.show({title: '为不影响正常使用，请录入个人信息',type: 'error'})
						}
						let data = {userInfo : {avatarUrl,nickName},token,userID,userType,wkID,pm_company};
						this.signing(data); //保存登录状态
					}
				})
				// #endif
			}
		}
	}
</script>

<style lang="scss">
	@import "./indexStyle.scss";
</style>
